<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\LiveRoom;
use App\Models\FishingSite;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class LiveRoomController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new LiveRoom(['user']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('user.name', '主播');
            $grid->column('site_id', '钓点')->display(function ($value) {
                return $value ? FishingSite::where('id', $this->site_id)->value('name') : '/';
            });
            $grid->column('name')->display(function() {
                if ($this->site_id) {
                    return $this->user->name . '【' . FishingSite::where('id', $this->site_id)->value('name') . '】';
                }
                return $this->user->name;
            });
            $grid->status()->radio([1 => '直播中', 2 => '已结束', 3 => '后台关闭']);

            $grid->column('link_keyword')->copyable()->width(800);
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('uid', '用户')->select(DB::table('users')->pluck('name', 'id'));
                $filter->equal('site_id', '钓点')->select(DB::table('fishing_sites')->pluck('name', 'id'));
                $filter->equal('status', '状态')->select([1 => '直播中', 2 => '已结束', 3 => '后台关闭']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new LiveRoom(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('link_keyword');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new LiveRoom(), function (Form $form) {
            $form->display('id');
            $form->select('uid', '主播')->options(DB::table('users')->pluck('name', 'id'))->required();
            $form->select('site_id', '钓点')->options(DB::table('fishing_sites')->pluck('name', 'id'));
            $form->text('link_keyword')->required();
            $form->select('status')->options([1 => '直播中', 2 => '已结束', 3 => '后台关闭'])->default(1)->required();
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
